function [trl, event] = trlfunDecodingPerception(cfg)

% experiment:
% -----------
% Subjects view stimuli of different categories (30 per category, 120 
% stimuli total, 4 blocked repeats). The task is to attend and
% press a button when fixation dot turns red. Stimuli are normalized for
% luminance and spatial frequency.

% trial structure:
% ----------------
% iti: 1500ms (trigger: 1) 
% stimulus: 2000ms (trigger: stimnr + 100) 
% blink period: 1500ms (trigger: stimulus category 21-25, see below)
%

%
% ~10% response trials.Color change can occur only during iti or stimulus 
% (random onset). Therefore, selecting trials based on mask trigger 
% automatically excludes response trials (as is being done with by this 
% function).
%
% stimulus category codes: 
% ------------------------
% 21 = face 
% 22 = scene 
% 23 = body 
% 24 = tool 
% 25 = word 

%% define trials

% extract hdr and event info
hdr   = ft_read_header(cfg.dataset);
event = ft_read_event(cfg.dataset);

% search for trigger events
value    = [event(strcmp('UPPT001', {event.type})).value]';
sample   = [event(strcmp('UPPT001', {event.type})).sample]';

% determine number of samples before and after trigger
preTrig             = -round(  (-3)  *  hdr.Fs);
postTrig            =  round(  (1)   *  hdr.Fs);
beamerCorrection    =  round(  0.05  *  hdr.Fs);
offs                =  -1.0;

% define trials based on triggers and create custom trl matrix
trl = [];

if strcmp(cfg.condition, 'lum_spat')

    for i=1:length(value) % loop over all events
        if value(i)>=21 && value(i)<=25   
            % define time interval
            beginSample   = sample(i) - preTrig + beamerCorrection;
            endSample     = sample(i) + postTrig + beamerCorrection;
            offset        = round(offs  *  hdr.Fs); % @ stimlus onset 
            stimType      = value(i);

            % creat trl matrix, condition in 4th column that will later be in
            % data.trialinfo structure
            trl(end+1, :) = round([beginSample endSample offset stimType]);
        end
    end
elseif strcmp(cfg.condition, 'lum')
    
    for i=1:length(value) % loop over all events
        if value(i)>=31 && value(i)<=35   
            % define time interval
            beginSample   = sample(i) - preTrig + beamerCorrection;
            endSample     = sample(i) + postTrig + beamerCorrection;
            offset        = round(offs  *  hdr.Fs); % @ stimlus onset 
            stimType      = value(i);

            % creat trl matrix, condition in 4th column that will later be in
            % data.trialinfo structure
            trl(end+1, :) = round([beginSample endSample offset stimType]);
        end
    end
else
    error('ERROR: Unknown condition identifier. Please specify ''lum'' or ''lum_spat''');
end

% sort trl matrix on sample number to prevent rejection bias
% this step is only important when selecting on condition (which is not
% done by the function in its current state). Still include the last line
% to be sure.
trl = sortrows(trl, 1);

end
